package org.example;

import java.sql.*;

//更新操作
public class Jdbc_Teacher_CRUD_Update {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mysqlbase?serverTimezone=GMT&characterEncoding=utf8";
        String user = "root";
        String password = "1234";
        String sql = "UPDATE teacher SET name = ? WHERE id = ?";
        try (Connection conn = DriverManager.getConnection(url, user, password)){
            conn.setAutoCommit(false);
            try (PreparedStatement ps = conn.prepareStatement(sql)){
                ps.setString(1, "李教授");
                ps.setInt(2, 1);
                ps.executeUpdate();
                conn.commit();
            }catch (SQLException e){
                conn.rollback();
                e.printStackTrace();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
